
#install.packages("rmarkdown")
library(rmarkdown)
#install.packages("knitr")
library(knitr)

install.packages("tinytex")
tinytex::install_tinytex()

version
#R version 4.1.3 (2022-03-10); system: x86_64, mingw32

#####install packages needed for the file######

library(stargazer)
packageVersion("stargazer") #'5.2.3'

#install.packages("lme4")
library(lme4)
packageVersion("lme4") #'1.1.31'

#install.packages("boot")
library(boot)
packageVersion("boot") #'1.3.28'

#install.packages("gplot2")
library(ggplot2)
packageVersion("ggplot2") #'3.4.0'

#install.packages("writexl")
library(writexl)
packageVersion("writexl") #'1.4.1'

#install.packages("multilevel")
library(multilevel)
packageVersion("multilevel") #'2.7'

#install.packages("nlme")
library(nlme)
packageVersion("nlme") #'3.1.161'

#install.packages("dfoptim")
library(dfoptim)
packageVersion("dfoptim") #'2020.10.1'

#install.packages("optimx")
library(optimx)
packageVersion("optimx") #'2022.4.30'

#install.packages("dplyr")
library(dplyr)
packageVersion("dplyr") #'1.0.10'

#install.packages("afex")
library(afex)
packageVersion("afex") #'1.2.0'

#install.packages("vcd")
library(vcd)
packageVersion("vcd") #'1.4.10'

#install.packages("plyr")
library(plyr)
packageVersion("plyr") #'1.8.8'

#install.packages("tidyverse")
library(tidyverse)
packageVersion("tidyverse") #'1.3.2'

#install.packages("magrittr")
library(magrittr)
packageVersion("magrittr") #'2.0.3'

#install.packages("DWwR")
#library(DMwR)

#install.packages("partykit")
library(partykit)
packageVersion("partykit") #'1.2.16'

#install.packages("pdp")
library(pdp)
packageVersion("pdp") #'0.8.1'

#install.packages("iml")
library(iml)
packageVersion("iml") #'0.11.1'

#install.packages("DALEX")
library(DALEX)
packageVersion("DALEX") #'2.4.2'

#install.packages("relaimpo")
library(relaimpo)
packageVersion("relaimpo") #'2.2.6'

#install.packages("mitml")
library(mitml)
packageVersion("mitml") #'0.4.3'

#install.packages("rr2")
library(rr2)
packageVersion("rr2") #'1.1.0'

#install.packages("r2mlm")
library(r2mlm)
packageVersion("relaimpo") #'2.2.6'

#install.packages("AER")
library(AER)
packageVersion("AER") #'1.2.10'

#install.packages("BayesPostEst")
library(BayesPostEst)
packageVersion("BayesPostEst") #'0.3.2'

#install.packages("rstanarm")
library(rstanarm)
packageVersion("rstanarm") #'2.21.4'

#install.packages("devtools")
library(devtools)
packageVersion("devtools") #'2.4.4'

#install.packages("foreign")
library(foreign)
packageVersion("foreign") #'0.8.84'

#install.packages("mvtnorm")
library(mvtnorm)
packageVersion("mvtnorm") #'1.1.3'

#install.packages("interplot")
library(interplot)
packageVersion("interplot") #'0.2.3'

#install.packages("ggforce")
library(ggforce)
packageVersion("ggforce") #'0.4.1'

##########load the data (change the path if necessary)#########

data1<-read.csv("C://isq//china bills 2009-2022.csv",fileEncoding="UTF-8-BOM")
View(data1)

############change the class of variables#################

data1$dw_nominate_dim1<-as.numeric(data1$dw_nominate_dim1)
data1$pages<-as.numeric(data1$pages)
data1$Legislation.type<-ifelse(data1$Legislation.type=="Bill","Bill","Resolution")
data1$term_has_served<-as.numeric(data1$term_has_served)
data1$month_since_beginning_congress<-as.numeric(data1$month_since_beginning_congress)

#########select legislation that describe China as a threat#######

data11<-subset(data1,data1$anti.china==1)
nrow(data11)  #603

# Create a new variable that counts the number of China-related legislative proposals introduced in each congressional session

table(data11$Congress)
number_of_obs <- ifelse(data11$Congress == "111th Congress (2009-2010)", 38,
                        ifelse(data11$Congress == "112th Congress (2011-2012)", 36,
                               ifelse(data11$Congress == "113th Congress (2013-2014)", 30,
                                      ifelse(data11$Congress == "114th Congress (2015-2016)", 23,
                                             ifelse(data11$Congress == "115th Congress (2017-2018)", 49,
                                                    ifelse(data11$Congress == "116th Congress (2019-2020)", 166,
                                                           ifelse(data11$Congress == "117th Congress (2021-2022)", 261, NA)))))))



data11$number_of_obs <- as.numeric(number_of_obs)

##############################
##########Table 2#############
##############################

subset_data <- data11[!is.na(data11$dw_nominate_dim1),];nrow(subset_data)

#Sponsor's ideology
mean(abs(subset_data$dw_nominate_dim1));sd(abs(subset_data$dw_nominate_dim1));min(abs(subset_data$dw_nominate_dim1));max(abs(subset_data$dw_nominate_dim1))

#Sponsors' years of service
mean(subset_data$term_has_served);sd(subset_data$term_has_served);min(subset_data$term_has_served);max(subset_data$term_has_served)

#Legislative length 
mean(log(subset_data$pages));sd(log(subset_data$pages));min(log(subset_data$pages));max(log(subset_data$pages))

#Resolution
subset_data$Legislation.type<-ifelse(subset_data$Legislation.type=="Resolution",1,0)
mean(subset_data$Legislation.type);sd(subset_data$Legislation.type);min(subset_data$Legislation.type);max(subset_data$Legislation.type)

#Months have passed
mean(subset_data$month_since_beginning_congress);sd(subset_data$month_since_beginning_congress);min(subset_data$month_since_beginning_congress);max(subset_data$month_since_beginning_congress)

#Salience
mean(subset_data$number_of_obs);sd(subset_data$number_of_obs);min(subset_data$number_of_obs);max(subset_data$number_of_obs)

###############remove the legislation that deals with International Affairs######

data111<-subset(data11,data11$policy.area!="International Affairs")
nrow(data111)   #256

######################################
#######Models in the main text########
######################################

######create a binary variable for "International Affairs"#####

data11$ia<-ifelse(data11$egan_owner_party2=="International Affairs",1,0)

###############Model (1) in Table 3################

model1<-stan_glmer(formula = complete_partisan~
                     #bill-level variables
                     ia+
                     abs(data11$dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level variables
                     number_of_obs+
                     (1|Congress),
                     data=data11,seed=921,
                     family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)

######Write the simulated coefficients output to an Excel file (change the path if necessary)#####

#model_1<-as.data.frame(model1)
#write_xlsx(model_1,"C://isq//outside.xlsx")
######then save the file as a csv file#######


#######Model (2) in Table 3########

model2<-stan_glmer(formula = complete_partisan~
                     #bill-level variables
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level variables
                     number_of_obs+
                     (1|Congress),
                     data=data111,seed=921,
                     family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)

######Write the simulated coefficients output to an Excel file (change the path if necessary)#####

#data222<-as.data.frame(model2)
#write_xlsx(data222,"C://isq//owned.xlsx")
######then save the file as a csv file#######

######################
######Figure 1########
######################

table(data11$Congress)
data_number_by_congress<-data.frame(Congress=c("111th (2009-2010)","112th (2011-2012)","113th (2013-2014)",
                                               "114th (2015-2016)","115th (2017-2018)","116th (2019-2020)",
                                               "117th (2021-2022)"),
                                    number=c(38,36,30,23,49,166,261))
data_number_by_congress$Congress<-as.factor(data_number_by_congress$Congress)

windows()
ggplot(data = data_number_by_congress, mapping = aes(
  x = Congress, y = number )) +
  geom_col(width = 0.8) +
  xlab(" ") + ylab(" ") +
  theme(axis.line = element_line(colour = "black"),
        panel.background = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 21, face = "bold"),
        axis.text = element_text(size = 18, colour = "black"),
        axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 14)) +
  theme(panel.grid.major.x = element_blank()) +
  theme(panel.grid.major.y = element_line(colour = "grey28")) +
  ggtitle("Figure 1. The Number of China-related Legislation by Congress (2009-2022)") +
  scale_y_continuous(breaks = seq(0, 270, 30), limits = c(0, 270), minor_breaks = seq(0, 270, 30)) +
  labs(caption = "Note: The y-axis represents the number of measures. Legislation includes both bills (H.R./H.J.Res.) and resolutions (H.Con.Res./H.Res.)")+
  theme(plot.caption = element_text(size = 16, margin = margin(t = 20))) # Adjust the margin value as needed


######################
######Figure 2########
######################

data_number_by_policyarea <- data.frame( Frequency = table(data111$policy.area))

data_number_by_policyarea <- data_number_by_policyarea[order(-data_number_by_policyarea$Frequency.Freq),]

data_number_by_policyarea$Frequency.Var1<-factor(data_number_by_policyarea$Frequency.Var1,
                                                 levels = data_number_by_policyarea$Frequency.Var1[order(data_number_by_policyarea$Frequency.Freq)])

windows()
ggplot(data = data_number_by_policyarea, mapping = aes(
  x=Frequency.Var1, y = Frequency.Freq))+
  geom_col(width = 0.8)+
  xlab(" ")+ylab(" ")+
  theme(axis.line = element_line(colour = "black"),
        panel.background = element_blank(),
        plot.title = element_text(hjust = 0.5,size=21,face = "bold"),
        axis.text=element_text(size=18,colour = "black"),
        axis.title.y=element_blank())+
  theme(panel.grid.major.y = element_blank())+
  coord_flip()+
  theme(panel.grid.major.x = element_line(colour="grey28"))+
  ggtitle("Figure 2. The Number of China-related Legislation by \nPolicy Areas (2009-2022)")+
  scale_y_continuous(breaks = seq(0,70,10),limits = c(0,70),minor_breaks = seq(0,70,10))+
  labs(caption = "Note: The x-axis represents the number of measures. Legislation includes both bills (H.R./H.J.Res.) and resolutions (H.Con.Res./H.Res.)")+
  theme(plot.caption = element_text(size = 16)) # Adjust the margin value as needed


##########################
#######Figure 3###########
##########################

#####read the simulated coefficients output (change the path if necessary)#####

outside_coefficient<-read.csv("C://isq//outside.csv")
owned_coefficient<-read.csv("C://isq//owned.csv")

ia<-outside_coefficient$ia
owned<-owned_coefficient$ownership_r_and_rowned

windows()
layout(matrix(c(1, 2), 1, 2), widths=c(1, 1), heights=c(1))
hist(ia, xlab="Coefficient Value", main="3A. International Affairs", 
     ylab="Frequency", breaks = 30,ylim = c(0,500))
abline(v=0, col="red", lwd=1.5, lty=2)
hist(owned, xlab="Coefficient Value", main="3B. Owned By One or Two Parties", 
     ylab=" ", breaks = 30,ylim = c(0,500))
abline(v=0, col="red", lwd=1.5, lty=2)
mtext("Figure 3. Frequency Distribution of Simulated Coefficients Values", 
      line = -1.5, cex=1.5, outer = TRUE,font = 2)


#########################################
###Figure 4 (substantive significance)###
#########################################

##########Hypothesis 1###########
####Change the path as necessary####
outside_coefficient<-read.csv("C://isq//outside.csv");names(outside_coefficient)
outside_random_intercepts<-read.csv("C://isq//outside_randomcoefficients.csv")
subset_data <- data11[!is.na(data11$dw_nominate_dim1),];nrow(subset_data)

ideology<-abs(subset_data$dw_nominate_dim1)
terms<-subset_data$term_has_served
pages<-log(subset_data$pages)
resolution<-subset_data$Legislation.type
resolution<-ifelse(resolution=="Bill",0,1)
months<-subset_data$month_since_beginning_congress
salience<-subset_data$number_of_obs

######predicted probabilities for legislation dealing with domestic affairs #####

outside_house<-NULL
outside_house<-NA
outside_house<-vector(mode = "numeric",length=4000)

for (j in 1:4000) {
  outside<-NULL
  outside<-NA
  outside<-vector(mode = "numeric",length = nrow(subset_data))
  
  vector1<-as.numeric(outside_coefficient[j,])
  vector2<-as.numeric(outside_random_intercepts[,j+1])
  
  for (i in 1:nrow(subset_data)) {
    outside[i]<-plogis(
        0*vector1[2]+
        ideology[i]*vector1[3]+
        resolution[i]*vector1[4]+
        pages[i]*vector1[5]+
        terms[i]*vector1[6]+
        months[i]*vector1[7]+
        number_of_obs[i]*vector1[8]+
        vector1[1]+vector2[i]
    )
  }
  outside_house[j]<-mean(outside)
}

length(outside_house)
a<-sort(outside_house)

a[201];mean(a);a[3800] #0.57; 0.62; 0.67


######predicted probabilities for legislation dealing with international affairs #####

outside_house<-NULL
outside_house<-NA
outside_house<-vector(mode = "numeric",length=4000)

for (j in 1:4000) {
  outside<-NULL
  outside<-NA
  outside<-vector(mode = "numeric",length = nrow(subset_data))
  
  vector1<-as.numeric(outside_coefficient[j,])
  vector2<-as.numeric(outside_random_intercepts[,j+1])
  
  for (i in 1:nrow(subset_data)) {
    outside[i]<-plogis(
        1*vector1[2]+
        ideology[i]*vector1[3]+
        resolution[i]*vector1[4]+
        pages[i]*vector1[5]+
        terms[i]*vector1[6]+
        months[i]*vector1[7]+
        number_of_obs[i]*vector1[8]+
        vector1[1]+vector2[i]
    )
  }
  outside_house[j]<-mean(outside)
}

length(outside_house)
a<-sort(outside_house)

a[201];mean(a);a[3800] #0.43; 0.48; 0.52


##########Hypothesis 2###########
####Change the path as necessary####

owned_coefficient<-read.csv("C://isq//owned.csv")
owned_random_intercepts<-read.csv("C://isq//owned_randomcoefficients.csv")

ideology<-abs(data111$dw_nominate_dim1)
terms<-data111$term_has_served
pages<-log(data111$pages)
resolution<-data111$Legislation.type
resolution<-ifelse(resolution=="Bill",0,1)
months<-data111$month_since_beginning_congress
salience<-data111$number_of_obs

######predicted probabilities for dealing with issues owned by one or two parties#####

owned_house<-NULL
owned_house<-NA
owned_house<-vector(mode = "numeric",length=4000)

for (j in 1:4000) {
  owned<-NULL
  owned<-NA
  owned<-vector(mode = "numeric",length = nrow(data111))
  
  vector1<-as.numeric(owned_coefficient[j,])
  vector2<-as.numeric(owned_random_intercepts[,j+1])
  
  for (i in 1:nrow(data111)) {
    owned[i]<-plogis(
        1*vector1[2]+
        ideology[i]*vector1[3]+
        pages[i]*vector1[4]+
        resolution[i]*vector1[5]+
        terms[i]*vector1[6]+
        months[i]*vector1[7]+
        salience[i]*vector1[8]+
        vector1[1]+vector2[i]
    )
  }
  owned_house[j]<-mean(owned)
}

length(owned_house)
a<-sort(owned_house)

a[201];mean(a);a[3800] #0.65; 0.7; 0.75


######predicted probabilities for dealing with issues owned by neither party#####

owned_house<-NULL
owned_house<-NA
owned_house<-vector(mode = "numeric",length=4000)

for (j in 1:4000) {
  owned<-NULL
  owned<-NA
  owned<-vector(mode = "numeric",length = nrow(data111))
  
  vector1<-as.numeric(owned_coefficient[j,])
  vector2<-as.numeric(owned_random_intercepts[,j+1])
  
  for (i in 1:nrow(data111)) {
    owned[i]<-plogis(
        0*vector1[2]+
        ideology[i]*vector1[3]+
        pages[i]*vector1[4]+
        resolution[i]*vector1[5]+
        terms[i]*vector1[6]+
        months[i]*vector1[7]+
        salience[i]*vector1[8]+
        vector1[1]+vector2[i]
    )
  }
  owned_house[j]<-mean(owned)
}

length(owned_house)
a<-sort(owned_house)

a[201];mean(a);a[3800] #0.48; 0.58; 0.68


#####plot (change the path if necessary)#####

data_boxplot<-read.csv("C://isq//substantive significance.csv")
data_boxplot1<-data_boxplot[1:6,1:3]

plot1<-ggplot(data_boxplot1, aes(x=a, y=pred))+
  geom_point(position = position_dodge(width = 0.75),size=2.5,show.legend = FALSE,shape=15)+
  geom_line(position = position_dodge(width = 0.75))+theme_bw()+
  theme(axis.line = element_line(colour = "black"),
        panel.background = element_blank(),
        axis.text=element_text(size=13,colour = "black"),
        axis.title.x=element_blank(),
        plot.caption = element_text(size=16),
        legend.text = element_text(size=17),
        legend.title = element_text(size=17),
        plot.title = element_text(hjust = 0.5,size=15,face = "bold"),
        legend.spacing.x = unit(0.15,"cm"),
        legend.spacing.y = unit(0.1,"cm"),
        axis.title=element_text(size=15,face="bold"))+
  theme(legend.key.size = unit(1.3, 'cm'))+
  ylab(" ")+
  scale_y_continuous(breaks = seq(0.3,1,0.1),limits = c(0.3,1),minor_breaks = seq(0.3,1,.1))+
  theme(legend.position=c(0.15,0.16))+
  theme(panel.grid.major.x = element_blank())+
  theme(panel.grid.major.y = element_line(colour="grey28"))+
  scale_fill_brewer(palette = "Set3")+
  guides(size = guide_legend(nrow = 1))+
  #labs(x = "Bill length")+
  theme(plot.caption = element_text(hjust = 0)) +
  ggtitle("A. Domestic/International Affairs")+
  #ggtitle("A. Predicted Probabilities of Being Highly Partisan")+
  guides(shape = guide_legend(override.aes = list(size = 5)))+
  scale_x_discrete(labels = function(a) str_wrap(a, width = 15))

windows()
plot1

##################################

data_boxplot2<-data_boxplot[7:12,1:3]
View(data_boxplot2)

plot2<-ggplot(data_boxplot2, aes(x=a, y=pred))+
  geom_point(position = position_dodge(width = 0.75),size=2.5,show.legend = FALSE,shape=15)+
  geom_line(position = position_dodge(width = 0.75))+theme_bw()+
  theme(axis.line = element_line(colour = "black"),
        panel.background = element_blank(),
        axis.text=element_text(size=13,colour = "black"),
        axis.title.x=element_blank(),
        plot.caption = element_text(size=16),
        legend.text = element_text(size=17),
        legend.title = element_text(size=17),
        plot.title = element_text(hjust = 0.5,size=15,face = "bold"),
        legend.spacing.x = unit(0.15,"cm"),
        legend.spacing.y = unit(0.1,"cm"),
        axis.title=element_text(size=15,face="bold"))+
  theme(legend.key.size = unit(1.3, 'cm'))+
  ylab(" ")+
  scale_y_continuous(breaks = seq(0.3,1,0.1),limits = c(0.3,1),minor_breaks = seq(0.3,1,.1))+
  theme(legend.position=c(0.15,0.16))+
  theme(panel.grid.major.x = element_blank())+
  theme(panel.grid.major.y = element_line(colour="grey28"))+
  scale_fill_brewer(palette = "Set3")+
  guides(size = guide_legend(nrow = 1))+
  #labs(x = "Bill length")+
  theme(plot.caption = element_text(hjust = 0)) +
  ggtitle("B. Issue Ownership")+
  #ggtitle("A. Predicted Probabilities of Being Highly Partisan")+
  guides(shape = guide_legend(override.aes = list(size = 5)))+
  scale_x_discrete(labels = function(a) str_wrap(a, width = 15))

windows()
plot2


library(ggpubr)
#install.packages("stringr")          # Install stringr package
library(stringr)   
windows()
taiwan_figure<-ggarrange(plot1,plot2,
                         ncol = 2, nrow = 1)
annotate_figure(taiwan_figure,top = text_grob("Figure 4. Statistical Simulation Results for Predicted Probabilities of China-Related Legislation \nBeing Highly Partisan with 90% Credible Intervals",face = "bold",size = 19),
                bottom = text_grob("Note: Figure 4A and 4B are based on Model (1) and (2) in Table 3, respectively. Credible intervals are calculated from 4,000 simulations. \n The dependent variable is whether all cosponsors are from the sponsor's party. Measures are coded 1 if yes and 0 otherwise."
                                   
                                   ,hjust=1,x=1,size = 15))



####################################################
#######################Appendix#####################
####################################################


####Table A3#### 

model1<-stan_glmer(formula = complete_partisan4~
                     ia+
                     abs(dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


model2<-stan_glmer(formula = complete_partisan4~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111,seed=921,
                   family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)


####Table A4#### 

model1<-stan_glmer(formula = complete_partisan3~
                     ia+
                     abs(dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


model2<-stan_glmer(formula = complete_partisan3~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111,seed=921,
                   family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)


####Table A5#### 

model1<-stan_glmer(formula = complete_partisan2~
                     ia+
                     abs(dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


model2<-stan_glmer(formula = complete_partisan2~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111,seed=921,
                   family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)


###Table A6####
####Change the path if necessary####

data1<-read.csv("C://isq//china bills 2009-2022.csv",fileEncoding="UTF-8-BOM")

data1 <- subset(data1, !policy.area %in% c("Foreign Trade and International Finance",
                                           "Armed Service and National Security"))

data1$dw_nominate_dim1<-as.numeric(data1$dw_nominate_dim1)
data1$pages<-as.numeric(data1$pages)
data1$Legislation.type<-ifelse(data1$Legislation.type=="Bill","Bill","Resolution")
data1$term_has_served<-as.numeric(data1$term_has_served)
data1$month_since_beginning_congress<-as.numeric(data1$month_since_beginning_congress)

data11<-subset(data1,data1$anti.china==1)
nrow(data11)  #540

# Create a vector of the desired values for number_of_obs based on Congress
table(data11$Congress)
number_of_obs <- ifelse(data11$Congress == "111th Congress (2009-2010)", 38,
                        ifelse(data11$Congress == "112th Congress (2011-2012)", 36,
                               ifelse(data11$Congress == "113th Congress (2013-2014)", 30,
                                      ifelse(data11$Congress == "114th Congress (2015-2016)", 23,
                                             ifelse(data11$Congress == "115th Congress (2017-2018)", 49,
                                                    ifelse(data11$Congress == "116th Congress (2019-2020)", 166,
                                                           ifelse(data11$Congress == "117th Congress (2021-2022)", 261, NA)))))))



data11$number_of_obs <- as.numeric(number_of_obs)

data111<-subset(data11,data11$policy.area!="International Affairs")
nrow(data111)   #193

data11$ia<-ifelse(data11$egan_owner_party2=="International Affairs",1,0)

model1<-stan_glmer(formula = complete_partisan~
                     ia+
                     abs(data11$dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                     data=data11,seed=921,
                     family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


#combine "owned by both parties" and owned by either Democrats and Republicans" into one category
model2<-stan_glmer(formula = complete_partisan~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                     data=data111,seed=921,
                     family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)


#####Table A7#####
##Change the path if necessary#####

data1<-read.csv("C://isq//china bills 2009-2022.csv",fileEncoding="UTF-8-BOM")

data1$dw_nominate_dim1<-as.numeric(data1$dw_nominate_dim1)
data1$pages<-as.numeric(data1$pages)
data1$Legislation.type<-ifelse(data1$Legislation.type=="Bill","Bill","Resolution")
data1$term_has_served<-as.numeric(data1$term_has_served)
data1$month_since_beginning_congress<-as.numeric(data1$month_since_beginning_congress)

data11<-subset(data1,data1$anti.china==1)
nrow(data11)  #603

# Create a vector of the desired values for number_of_obs based on Congress
table(data11$Congress)
number_of_obs <- ifelse(data11$Congress == "111th Congress (2009-2010)", 38,
                        ifelse(data11$Congress == "112th Congress (2011-2012)", 36,
                               ifelse(data11$Congress == "113th Congress (2013-2014)", 30,
                                      ifelse(data11$Congress == "114th Congress (2015-2016)", 23,
                                             ifelse(data11$Congress == "115th Congress (2017-2018)", 49,
                                                    ifelse(data11$Congress == "116th Congress (2019-2020)", 166,
                                                           ifelse(data11$Congress == "117th Congress (2021-2022)", 261, NA)))))))



data11$number_of_obs <- as.numeric(number_of_obs)


data111 <- subset(data11, !policy.area %in% c("Foreign Trade and International Finance",
                                              "Armed Service and National Security",
                                              "International Affairs"))
nrow(data111)   #193


data11$ia <- ifelse(data11$policy.area %in% c("International Affairs",
                                              "Foreign Trade and International Finance",
                                              "Armed Forces and National Security"), 1, 0)

table(data11$ia)

model1<-stan_glmer(formula = complete_partisan~
                     ia+
                     abs(data11$dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


#combine "owned by both parties" and owned by either Democrats and Republicans" into one category
model2<-stan_glmer(formula = complete_partisan~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111,seed=921,
                   family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)



#####Table A8#####
########change the pathway if necessary#######

data1<-read.csv("C://isq//china bills 2009-2022.csv",fileEncoding="UTF-8-BOM")

data1$dw_nominate_dim1<-as.numeric(data1$dw_nominate_dim1)
data1$pages<-as.numeric(data1$pages)
data1$Legislation.type<-ifelse(data1$Legislation.type=="Bill","Bill","Resolution")
data1$term_has_served<-as.numeric(data1$term_has_served)
data1$month_since_beginning_congress<-as.numeric(data1$month_since_beginning_congress)

data11 <- subset(data1, anti.china %in% c(1, "Cooperation", "Exchanges", "Kindness",
                                          "Trade","Trade Expansion","Immigration Visas"))
nrow(data11) #643

# Create a vector of the desired values for number_of_obs based on Congress
table(data11$Congress)
number_of_obs <- ifelse(data11$Congress == "111th Congress (2009-2010)", 45,
                        ifelse(data11$Congress == "112th Congress (2011-2012)", 48,
                               ifelse(data11$Congress == "113th Congress (2013-2014)", 37,
                                      ifelse(data11$Congress == "114th Congress (2015-2016)", 27,
                                             ifelse(data11$Congress == "115th Congress (2017-2018)", 53,
                                                    ifelse(data11$Congress == "116th Congress (2019-2020)", 170,
                                                           ifelse(data11$Congress == "117th Congress (2021-2022)", 263, NA)))))))



data11$number_of_obs <- as.numeric(number_of_obs)
data111<-subset(data11,data11$policy.area!="International Affairs")
data11$ia<-ifelse(data11$egan_owner_party2=="International Affairs",1,0)
model1<-stan_glmer(formula = complete_partisan~
                     ia+
                     abs(data11$dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)

#combine "owned by both parties" and owned by either Democrats and Republicans" into one category
model2<-stan_glmer(formula = complete_partisan~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111,seed=921,
                   family = binomial(link = "logit"))

summary(model2,probs=c(0.05,0.95),digits = 3)


#####Table A9: remove legislation with no cosponsors

data1<-read.csv("C://isq//china bills 2009-2022.csv",fileEncoding="UTF-8-BOM")

data1$dw_nominate_dim1<-as.numeric(data1$dw_nominate_dim1)
data1$pages<-as.numeric(data1$pages)
data1$Legislation.type<-ifelse(data1$Legislation.type=="Bill","Bill","Resolution")
data1$term_has_served<-as.numeric(data1$term_has_served)
data1$month_since_beginning_congress<-as.numeric(data1$month_since_beginning_congress)

data11<-subset(data1,data1$anti.china==1)
nrow(data11)  #603
table(data11$Congress)
number_of_obs <- ifelse(data11$Congress == "111th Congress (2009-2010)", 38,
                        ifelse(data11$Congress == "112th Congress (2011-2012)", 36,
                               ifelse(data11$Congress == "113th Congress (2013-2014)", 30,
                                      ifelse(data11$Congress == "114th Congress (2015-2016)", 23,
                                             ifelse(data11$Congress == "115th Congress (2017-2018)", 49,
                                                    ifelse(data11$Congress == "116th Congress (2019-2020)", 166,
                                                           ifelse(data11$Congress == "117th Congress (2021-2022)", 261, NA)))))))



data11$number_of_obs <- as.numeric(number_of_obs)
data111<-subset(data11,data11$policy.area!="International Affairs")
nrow(data111)   #256

data11_0<-subset(data11,data11$Number.of.Cosponsors>0)
data11_0$ia<-ifelse(data11_0$egan_owner_party2=="International Affairs",1,0)
data111_0<-subset(data111,data111$Number.of.Cosponsors>0);nrow(data111_0)

model1<-stan_glmer(formula = complete_partisan~
                     ia+
                     abs(data11_0$dw_nominate_dim1)+
                     Legislation.type+
                     log(pages)+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data11_0,seed=921,
                   family = binomial(link = "logit"))

summary(model1,probs=c(0.05,0.95),digits = 3)


model2<-stan_glmer(formula = complete_partisan~
                     #bill-level
                     ownership_r_and_r+
                     abs(dw_nominate_dim1)+
                     
                     log(pages)+
                     Legislation.type+
                     term_has_served+
                     month_since_beginning_congress+
                     
                     #group-level
                     number_of_obs+
                     (1|Congress),
                   data=data111_0,seed=921,
                   family = binomial(link = "logit"))


summary(model2,probs=c(0.05,0.95),digits = 3)


